<template>
  <el-container>
    <el-header>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商家中心</el-breadcrumb-item>
        <el-breadcrumb-item>评价图表</el-breadcrumb-item>
      </el-breadcrumb>
    </el-header>
    <el-main>
      <el-card>
        <el-tabs type="border-card">
          <el-tab-pane label="菜品评价">
            <div id="main1" class="main"></div>
          </el-tab-pane>
          <el-tab-pane label="菜品评价占比">
            <div id="main2" class="main"></div>
          </el-tab-pane>
          <el-tab-pane label="菜品评分趋势">
            <div id="main3" class="main"></div>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </el-main>
  </el-container>
</template>

<script>
import echarts from 'echarts'

export default {
  name: 'Report',
  data () {
    return {
      option1: {
        title: {
          text: '菜品评价'
        },
        tooltip: {},
        legend: {
          data: ['麻辣', '辣度', '鲜度', '苦度', '咸度', '酸度', '甜度', '评分']
        },
        xAxis: {
          data: []
        },
        yAxis: {},
        series: [
          { name: '麻辣', type: 'bar', data: [] },
          { name: '辣度', type: 'bar', data: [] },
          { name: '鲜度', type: 'bar', data: [] },
          { name: '苦度', type: 'bar', data: [] },
          { name: '咸度', type: 'bar', data: [] },
          { name: '酸度', type: 'bar', data: [] },
          { name: '甜度', type: 'bar', data: [] },
          { name: '评分', type: 'bar', data: [] }
        ]
      },
      option2: {
        title: {
          text: '菜品评价占比'
        },
        series: [
          {
            name: '评价占比',
            type: 'pie',
            roseType: 'angle',
            radius: '55%',
            data: []
          }
        ]
      },
      option3: {
        title: {
          text: '菜品评分趋势'
        },
        legend: {
          data: ['评分', '评分趋势']
        },
        xAxis: [
          {
            data: []
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '评分',
            min: 0,
            max: 5,
            interval: 1
          }
        ],
        series: [
          {
            name: '评分',
            type: 'bar',
            data: [],
            itemStyle: {
              color: '#91cc75'
            }
          },
          {
            name: '评分趋势',
            type: 'line',
            data: [],
            itemStyle: {
              color: '#5470c6'
            }
          }
        ]
      }
    }
  },
  async mounted () {
    const myChart1 = echarts.init(document.getElementById('main1'))
    const myChart2 = echarts.init(document.getElementById('main2'))
    const myChart3 = echarts.init(document.getElementById('main3'))
    const rid = { rid: window.sessionStorage.getItem('rid') }
    const { data: res } = await this.$axios.get('/admin/restaurant/queryByID', { params: rid })
    if (res.status !== 2000) {
      return this.$message.error('获取数据失败')
    }
    const nameList = []
    const mspicy = []
    const spicy = []
    const fresh = []
    const bitter = []
    const salt = []
    const sour = []
    const sweet = []
    const commentList = []
    const dataList = []
    res.data.foods.forEach(function (item) {
      nameList.push(item.fname)
      mspicy.push(item.mspicy)
      spicy.push(item.spicy)
      fresh.push(item.fresh)
      bitter.push(item.bitter)
      salt.push(item.salt)
      sour.push(item.sour)
      sweet.push(item.sweet)
      commentList.push(item.comment)
      dataList.push({ value: item.count, name: item.fname })
    })
    this.option1.xAxis.data = nameList
    this.option1.series[0].data = mspicy
    this.option1.series[1].data = spicy
    this.option1.series[2].data = fresh
    this.option1.series[3].data = bitter
    this.option1.series[4].data = salt
    this.option1.series[5].data = sour
    this.option1.series[6].data = sweet
    this.option1.series[7].data = commentList
    this.option2.series[0].data = dataList
    const { data: res1 } = await this.$axios.get('/admin/restaurant/getComments', { params: rid })
    if (res1.status !== 2000) {
      return this.$message.error('获取数据失败')
    }
    this.option3.xAxis[0].data = res1.data.months
    this.option3.series[0].data = res1.data.comments
    this.option3.series[1].data = res1.data.comments
    myChart1.setOption(this.option1)
    myChart2.setOption(this.option2)
    myChart3.setOption(this.option3)
  }
}
</script>

<style scoped>
.el-header {
  height: 20px!important;
}
.main {
  margin-top: 30px;
  width: 900px;
  height: 400px;
}
</style>
